<template>
  <div class="articaldetail">
    <div class="header">
      <div class="left" @click="$router.back()">
        <van-icon name="arrow-left back" />
        <span class="iconfont iconnew new"></span>
      </div>
      <div class="author">
        <img :src="post.user && post.user.head_img" alt="" />
        <span style="margin-left: 2px">{{
          post.user && post.user.nickname
        }}</span>
      </div>
      <span :class="{ active: post.has_follow }" @click="btn_follow">{{
        post.has_follow ? "已关注" : "关注"
      }}</span>

      <van-dialog
        style="padding: 0 30px"
        v-model="follow_show"
        title="还未登录,是否去登录?"
        show-cancel-button
        confirmButtonColor="red"
        confirmButtonText="去登录"
        @confirm="toLogin"
      >
      </van-dialog>
    </div>
    <div class="detail">
      <div class="title">{{ post.title }}</div>
      <div class="desc">
        <span>{{ post.user && post.user.nickname }}</span> &nbsp;&nbsp;
        <span>{{ post.user && post.user.create_date }}</span>
      </div>

      <!-- 判断内容类型 -->
      <div v-if="post.type == 1" class="content" v-html="post.content"></div>
      <video
        v-if="post.type == 2"
        :src="post.content"
        controls
        class="content_video"
        poster="https://gimg2.baidu.com/image_search/src=http%3A%2F%2F5b0988e595225.cdn.sohucs.com%2Fimages%2F20180501%2F2750ff0918da40d2b801cbb5374eb5e6.jpeg&refer=http%3A%2F%2F5b0988e595225.cdn.sohucs.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1614268749&t=5795f258eaa7c2439c28cad79369dc67"
      ></video>
      <!-- 判断内容类型 -->

      <div class="opt">
        <span class="like" :class="{ active: post.has_like }" @click="btn_like">
          <van-icon class="van-icon" name="good-job-o" />{{
            post.has_like ? "已点赞" : "点赞"
          }}
        </span>
        <van-dialog
          style="padding: 0 30px"
          v-model="like_show"
          title="还未登录,是否去登录?"
          show-cancel-button
          confirmButtonColor="red"
          confirmButtonText="去登录"
          @confirm="toLogin"
        >
        </van-dialog>
        <span class="chat"> <van-icon name="chat" class="w" />微信 </span>
      </div>
    </div>
    <!-- 精彩跟帖 -->
    <div class="keeps">
      <h2>精彩跟帖</h2>

      <!-- 评论列表 -->
      <div class="lists">
        <div
          :key="index"
          class="item"
          :v-if="index <= 4"
          v-for="(item, index) in commentList"
        >
          <div class="head">
            <img :src="item.user.head_img" alt />
            <div>
              <p>{{ item.user.nickname }}</p>
              <span>{{ item.create_date }}</span>
            </div>
            <!-- <span>回复</span> -->
          </div>

          <div class="text">{{ item.content }}</div>
        </div>
      </div>

      <div class="more" @click="$router.push({ path: '/comment/' + post.id })">
        更多跟帖
      </div>
    </div>
    <itConmentsFooter
      @reLoad="init"
      @click_star="star"
      :star_active="post.has_star"
      :post="post"
    ></itConmentsFooter>
    <!-- 收藏弹出框 -->
    <van-dialog
      style="padding: 0 30px"
      v-model="star_show"
      title="还未登录,是否去登录?"
      show-cancel-button
      confirmButtonColor="red"
      confirmButtonText="去登录"
      @confirm="toLogin"
    >
    </van-dialog>
  </div>
</template>

// 脚本
<script>
// 文章aips    get_articleDetail(获取文章详情), post_like(点赞)), post_comment(评论列表)
import { get_articleDetail, post_like, post_comment } from "@/apis/post.js";
// 用户aips    user_follows(关注), user_unfollow(取消关注), article_star(收藏文章)
import { user_follows, user_unfollow, article_star } from "@/apis/user.js";
// 框架：moment 处理事件
import moment from "moment";
// 引入myAxios
import myaxios from "@/utils/myAxios.js";
// 自定义组件：itConmentsFooter
import itConmentsFooter from "@/components/itCommentsFooter.vue";

import login from "@/views/login.vue";

export default {
  // 注册组件
  components: {
    itConmentsFooter,
    login,
  },
  // 数据
  data() {
    return {
      post: { n: "n" }, //文章数据对象
      //post 一开始的{n: 'n'} 是没有任何意义的, 仅仅是为了让它一开始不是一个空的对象

      follow_show: false, //关注按钮弹出框
      like_show: false, //点赞按钮弹出框
      star_show: false, //收藏按钮弹出框

      commentList: [], //评论列表
    };
  },
  // 方法集合
  methods: {
    // 去登录
    toLogin() {
      this.$router.push({ path: "/login" });
    },
    // 功能：关注/取消
    async btn_follow() {
      console.log(this.post);

      // 先看看用户是否登录
      let userInfo = JSON.parse(localStorage.getItem("userInfo"));

      // 如果获取不到用户信息, 那就弹出询问用户是否登录的框框,恶心他
      if (!userInfo) {
        this.follow_show = true;
      } else {
        // 如果为true, 也就是关注了
        if (this.post.has_follow) {
          // 如果关注了, 就进行取关处理
          let res = await user_unfollow(this.post.user.id);
          if (res.status == 200) {
            this.post.has_follow = !this.post.has_follow;
          }
        } else {
          // 否则就进行关注处理
          let res = await user_follows(this.post.user.id);
          if (res.status == 200) {
            this.post.has_follow = !this.post.has_follow;
          }
        }
      }
    },
    // 功能：点赞/取消
    async btn_like() {
      // 先看看用户是否登录
      let userInfo = JSON.parse(localStorage.getItem("userInfo"));

      // 如果获取不到用户信息, 那就弹出询问用户是否登录的框框,恶心他
      if (!userInfo) {
        this.like_show = true;
      } else {
        // 点赞和取消点赞同为一个请求
        let res = await post_like(this.post.id);
        // console.log(res);
        if (res.status == 200 && res.data.message == "点赞成功") {
          this.post.has_like = true;
        } else if (res.status == 200 && res.data.message == "取消成功") {
          this.post.has_like = false;
        }
      }
    },
    // 功能：收藏/取消
    async star() {
      // 先看看用户是否登录
      let userInfo = JSON.parse(localStorage.getItem("userInfo"));

      // 如果获取不到用户信息, 那就弹出询问用户是否登录的框框,恶心他
      if (!userInfo) {
        this.star_show = true;
      } else {
        let res = await article_star(this.post.id);
        console.log(res);
        if (res.status == 200 && res.data.message == "收藏成功") {
          this.post.has_star = true;
          this.$toast.success(res.data.message);
        } else if (res.status == 200 && res.data.message == "取消成功") {
          this.post.has_star = false;
          this.$toast.success(res.data.message);
        }
      }
    },

    // 初始化
    async init() {
      // 获取文章详情：
      let res = await get_articleDetail(this.$route.params.id);
      if (res.status == 200) {
        this.post = res.data.data;
        // 改造数据的时间格式：
        this.post.user.create_date = moment(this.post.user.create_date).format(
          "YYYY-MM-DD"
        );
        if (this.post.type == 2) {
          this.post.content = myaxios.defaults.baseURL + this.post.content;
        }
        // 改造作者图片路径
        this.post.user.head_img =
          myaxios.defaults.baseURL + this.post.user.head_img;
        // console.log(this.post);
      }

      // 获取评论列表
      let res_comment = await post_comment(this.post.id);
      console.log(res_comment);
      if (res_comment.status == 200) {
        // 将评论列表结果赋给this.commentList
        this.commentList = res_comment.data.data;
        // 利用moment框架 改造评论中的时间格式
        this.commentList.map((item) => {
          item.create_date = moment(item.create_date).fromNow();
          item.user.head_img = myaxios.defaults.baseURL + item.user.head_img;
        });
      }
      console.log(this.post);
    },
  },
  // 钩子函数
  async activated() {
    window.scroll(0, 0);
    // 根据id获取文章的详情，实现文章详情的动态渲染

    this.init();
  },
};
</script>


// 样式：
<style lang='less' scoped>
.articaldetail {
  background-color: rgb(242, 242, 242);
}
.header {
  padding: 0px 10px;
  height: 50px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid #ccc;
  > .left {
    height: 100%;
    vertical-align: middle;
    position: relative;
    .van-icon {
      position: absolute;
      top: 15px;
      left: 0;
    }
    > span {
      margin-left: 22px;
      font-size: 50px;
    }
  }
  > .author {
    img {
      height: 30px;
      width: 30px;
      border-radius: 50%;
      vertical-align: middle;
    }
    span {
      height: 30px;
      line-height: 30px;
    }
  }
  > span {
    padding: 5px 15px;
    background-color: #f00;
    color: #fff;
    text-align: center;
    border-radius: 15px;
    font-size: 13px;
    transition: all 0.5s;

    &.active {
      background-color: #cfcfcf;
    }
  }
}
.detail {
  padding: 15px;
  .title {
    font-size: 18px;
    font-weight: bold;
    padding: 10px 0;
  }
  .desc {
    line-height: 30px;
    color: #999;
    font-size: 13px;
  }
  .content {
    text-indent: 2em;
    line-height: 24px;
    font-size: 15px;
    padding-bottom: 30px;
    width: 100%;
    // text-align: center;
    /deep/ img {
      display: block;
      width: 300 * 100vw / 360;
      margin: 0 auto;
    }
  }
  .content_video {
    margin: 0 auto;
    width: 100%;
    // height: 170/360 * 100vw;
    > img {
      width: 100%;
      // height: 100%;
    }
  }
}

.opt {
  display: flex;
  justify-content: space-around;
  .like,
  .chat {
    // position: absolute;
    height: 25px;
    padding: 0 15px;
    font-size: 14px;
    line-height: 25px;
    text-align: center;
    border: 1px solid #ccc;
    border-radius: 15px;
    transition: all 0.4s;
  }
  .like.active {
    // transition: all 0.3s;
    color: red;
    .van-icon {
      transition: all 0.4s;
      transform: translateY(-5px);
    }
  }
  .w {
    color: rgb(84, 163, 5);
  }
}
.keeps {
  border-top: 5px solid #ddd;
  padding: 0 15px;
  > h2 {
    line-height: 50px;
    text-align: center;
  }
  .item {
    padding: 10px 0;
    border-bottom: 1px solid #ccc;
    .head {
      display: flex;
      justify-content: space-between;
      align-items: center;
      > img {
        width: 50/360 * 100vw;
        height: 50/360 * 100vw;
        display: block;
        border-radius: 50%;
      }
      > div {
        flex: 1;
        display: flex;
        flex-direction: column;
        margin-left: 10px;
        > span {
          font-size: 12px;
          color: #999;
          line-height: 25px;
        }
      }
      > span {
        color: #999;
        font-size: 13px;
      }
    }
    .text {
      font-size: 14px;
      color: #333;
      padding: 20px 0 10px 0;
    }
  }
  .more {
    width: 100px;
    height: 30px;
    line-height: 30px;
    text-align: center;
    border: 1px solid #ccc;
    border-radius: 15px;
    margin: 20px auto;
    font-size: 13px;
  }
}
</style>
